//
//  ViewController.m
//  PPTToImage
//
//  Created by 刘川 on 2019/7/18.
//  Copyright © 2019 alex. All rights reserved.
//

#import "ViewController.h"
#import "PPTToImageTool.h"
#import "ImageListView.h"

#import "PPTToImageToolinWKWeb.h"
#import "WordToImageToolinWKWeb.h"

#import "PDFToImageTool.h"

@interface ViewController ()

//@property (nonatomic, strong) PPTToImageTool *toImagetool;
@property (nonatomic, strong)WordToImageToolinWKWeb *toImagetool;

//@property (nonatomic, strong)PPTToImageToolinWKWeb *toImagetool;
@property (nonatomic, strong) ImageListView *imageListView;


@property (nonatomic,strong) PDFToImageTool *pdfTool;



@end

@implementation ViewController

- (IBAction)PPTToImageBtnClick:(id)sender {
    NSLog(@"%s",__func__);
    
    if (1) {
        
//        #define PDF_FILE_PATH [[NSBundle mainBundle] pathForResource:@"git-cheatsheet" ofType:@"pdf"]

        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"003" ofType:@"pdf"];
        self.pdfTool = [PDFToImageTool new];
        
        __weak __typeof(self)weakSelf = self;

        [self.pdfTool PdfToImageWithPDFFileUrl:filePath progress:^(CGFloat value) {
            NSLog(@"进度-------%f", value);

        } completion:^(NSArray * _Nonnull images) {

            NSLog(@"转换后图片信息---%ld----%@",images.count,images);
            weakSelf.imageListView.images = images;
            [weakSelf.imageListView reloadData];
        }];

        
        return;
    }
    // 获取本地 ppt url 地址
//    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"IOS33.pptx" ofType:nil];
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"深圳腾千里科技有限公司-计算机软件工(1).docx" ofType:nil];

    NSString *lastname = [filePath lastPathComponent];
    NSLog(@"lastname %@",lastname);
    NSString *ext = filePath.pathExtension;
    NSLog(@"ext = %@",ext);
    
    
    // block 内部使用__weakself
    __weak __typeof(self)weakSelf = self;
    self.toImagetool = [WordToImageToolinWKWeb new];
    self.toImagetool.parentView = self.view;
    // 进行 ppt 转换
    [self.toImagetool pptToImageWithPPTFileUrl:filePath
                                      progress:^(CGFloat value) {
                                          
                                          NSLog(@"进度-------%f", value);
                                          
                                      } completion:^(NSArray * _Nonnull images) {
                                          
                                          NSLog(@"转换后图片信息---%ld----%@",images.count,images);
                                          weakSelf.imageListView.images = images;
                                          [weakSelf.imageListView reloadData];
                                      }];
}

- (ImageListView *)imageListView {
    if (!_imageListView) {
        _imageListView = [[ImageListView alloc]initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
        [self.view addSubview:_imageListView];
    }
    return _imageListView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@",[self nameStr]);//是需要获取异步得到的数值，以供同步来使用。
    NSLog(@"next");
}
#pragma mark - 异步改同步
- (NSString *)nameStr{

   __block NSString  * nameStr = @"小红";

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [self method:^(NSString *name) {
        
        nameStr = name;
        dispatch_semaphore_signal(semaphore);

    }];

    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

    return nameStr;
}

- (void)method:(void(^)(NSString * name))success_block{
    
    dispatch_queue_t   queue = dispatch_queue_create("com.queue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        if (success_block) {
            success_block(@"小明");
        }
    });
}
@end
